<template>
  <div class="home">
    <div class="head">
      <div class="back" @click="back">&lt; 返回</div>
      <p class="title">商品详情</p>
    </div>
    <div class="shop-box">
      <div class="img-box">
        <img :src="dataObj.large_img" alt="" />
      </div>
      <div class="shop-message">
        <div class="shop-head">
          <div class="shop-name">
            <div class="name">{{ dataObj.name }}</div>
            <div class="enname">{{ dataObj.enname }}</div>
          </div>
          <div style="color: rgb(12, 52, 186)">￥{{ dataObj.price }}</div>
        </div>
        <div class="shop-sel-box">
          <div v-for="(v, i) in dataArr" :key="i" class="shop-sel">
            <div class="shop-sel-title">{{ v.title }}</div>
            <div
              v-for="(value, index) in v.rule"
              :key="index"
              class="shop-sel-item"
              :class="{ 'shop-sel-active': v.activeIndex === index }"
              @click="activeChange(i, index)"
            >
              {{ value }}
            </div>
          </div>
        </div>
        <div class="shop-num">
          <div>选择数量</div>
          <div class="shop-sel-num">
            <div
              class="reduce"
              :class="shopNum < 2 ? '' : 'allow'"
              @click="reduce"
            >
              -
            </div>
            {{ shopNum }}
            <div class="add" :class="shopNum < 99 ? 'allow' : ''" @click="add">
              +
            </div>
          </div>
        </div>
        <div class="shop-content">
          <p>商品描述</p>
          <p>{{ dataObj.desc }}</p>
        </div>
      </div>
    </div>
    <div class="bottom-box">
      <van-goods-action>
        <van-goods-action-icon
          icon="bag"
          text="购物袋"
          :badge="ShopbagNum"
          :color="ShopbagNum > 0 ? 'rgb(12,52,186)' : '#000'"
          @click="goShopbag"
        />
        <van-goods-action-icon
          icon="like"
          :text="isCollect ? '收藏' : '未收藏'"
          :color="isCollect ? 'rgb(12,52,186)' : '#000'"
          @click="collect"
        />
        <van-goods-action-button
          color="rgb(106,145,236)"
          type="warning"
          text="加入购物车"
          @click="addShopCar"
        />
        <van-goods-action-button
          color="rgb(12,52,186)"
          type="danger"
          text="立即购买"
          @click="goIndent"
        />
      </van-goods-action>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  name: "shopMes",
  data() {
    return {
      dataObj: {},
      dataArr: [],
      shopNum: 1,
      pid: "",
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      isCollect: false,
      ShopbagNum: null,
    };
  },
  methods: {
    getPid(pid) {
      let appkey = this.appkey;
      this.axios({
        method: "get",
        url: "http://www.kangliuyong.com:10002/productDetail",
        params: {
          appkey,
          pid,
        },
      })
        .then((result) => {
          this.dataObj = result.data.result[0];
          
          this.start();
        })
        .catch((err) => {
          
        });
    },
    start() {
      let arr = ["cream", "milk", "sugar", "tem"];
      arr.forEach((v) => {
        if (this.dataObj[v]) {
          let rule = this.dataObj[v].split("/");
          let title = this.dataObj[`${v}_desc`];
          this.dataArr.push({
            title,
            activeIndex: 0,
            rule,
          });
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    activeChange(a, b) {
      this.dataArr[a].activeIndex = b;
    },
    reduce() {
      if (this.shopNum > 1) {
        this.shopNum--;
      }
    },
    add() {
      if (this.shopNum < 99) {
        this.shopNum++;
      }
    },
    collect() {
      let tokenString = sessionStorage.getItem("userToken");
      if (!tokenString) {
        Toast("请先登录");
      } else {
        let appkey = this.appkey;
        let pid = this.pid;
        let data = `appkey=${appkey}&pid=${pid}&tokenString=${tokenString}`;
        if (this.isCollect) {
          this.axios({
            method: "post",
            url: "http://www.kangliuyong.com:10002/notlike",
            data,
          })
            .then((res) => {
              
              if (res.data.code === 900) {
                Toast("已取消收藏");
                this.isCollect = false;
              }
            })
            .catch((err) => {
              
            });
        } else {
          this.axios({
            method: "post",
            url: "http://www.kangliuyong.com:10002/like",
            data,
          })
            .then((res) => {
              
              if (res.data.code === 800) {
                Toast("收藏成功");
                this.isCollect = true;
              }
            })
            .catch((err) => {
              
            });
        }
      }
    },
    isCollectHas() {
      let tokenString = sessionStorage.getItem("userToken");
      let appkey = this.appkey;
      let pid = this.pid;
      this.axios({
        method: "get",
        url: "http://www.kangliuyong.com:10002/findlike",
        params: {
          appkey,
          pid,
          tokenString,
        },
      })
        .then((res) => {
          if (res.data.result.length > 0) {
            this.isCollect = true;
          } else {
            this.isCollect = false;
          }
        })
        .catch((err) => {
          
        });
    },
    addShopCar() {
      let tokenString = sessionStorage.getItem("userToken");
      if (!tokenString) {
        Toast("请先登录");
      } else {
        let appkey = this.appkey;
        let pid = this.pid;
        let count = this.shopNum;
        let rule = "";
        this.dataArr.forEach((v) => {
          rule += v.rule[v.activeIndex] + "/";
        });
        rule = rule.slice(0, -1);
        let data = `appkey=${appkey}&pid=${pid}&tokenString=${tokenString}&count=${count}&rule=${rule}`;
        this.axios({
          method: "post",
          url: "http://www.kangliuyong.com:10002/addShopcart",
          data,
        })
          .then((res) => {
            
            if (res.data.code === 3000) {
              Toast("加入购物车成功");
              this.getShopbagNum();
            }
          })
          .catch((err) => {
            
          });
      }
    },
    getShopbagNum() {
      let tokenString = sessionStorage.getItem("userToken");
      let appkey = this.appkey;
      this.axios({
        method: "get",
        url: "http://www.kangliuyong.com:10002/shopcartCount",
        params: {
          appkey,
          tokenString,
        },
      })
        .then((result) => {
          
          this.ShopbagNum = result.data.result;
        })
        .catch((err) => {
          
        });
    },
    goShopbag() {
      let tokenString = sessionStorage.getItem("userToken");
      if (!tokenString) {
        Toast("请先登录");
      } else {
        this.$router.push({ name: "Shopbag" });
      }
    },
    goIndent() {
      let tokenString = sessionStorage.getItem("userToken");
      if (!tokenString) {
        Toast("请先登录");
      } else {
        let appkey = this.appkey;
        let pid = this.pid;
        let count = this.shopNum;
        let rule = "";
        this.dataArr.forEach((v) => {
          rule += v.rule[v.activeIndex] + "/";
        });
        rule = rule.slice(0, -1);
        let data = `appkey=${appkey}&pid=${pid}&tokenString=${tokenString}&count=${count}&rule=${rule}`;
        this.axios({
          method: "post",
          url: "http://www.kangliuyong.com:10002/addShopcart",
          data,
        })
          .then((res) => {
            
            if (res.data.code === 3000) {
              this.$router.push({ name: "Shopbag" });
            }
          })
          .catch((err) => {
            
          });
      }
    },
  },
  created() {
    let pid = this.$route.query.pid;
    this.pid = pid;
    this.getPid(pid);
    this.isCollectHas();
    this.getShopbagNum();
  },
};
</script>

<style lang="less" scoped>
.home {
  text-align: center;
  font-size: 16px;
  .head {
    width: 100%;
    height: 40px;
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgba(255, 255, 255, 0.5);
    z-index: 9;
    .title {
      margin: 0;
      line-height: 40px;
      font-size: 20px;
    }
    .back {
      height: 100%;
      line-height: 40px;
      color: rgb(0, 119, 255);
      padding: 0 10px;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  .shop-box {
    width: 100%;
    padding-top: 40px;
    .shop-message {
      width: 90%;
      min-height: 60%;
      padding-bottom: 90px;
      box-sizing: border-box;
      background-color: #fff;
      box-shadow: 0 0px 1px 1px #ddd;
      border-top-left-radius: 16px;
      border-top-right-radius: 16px;

      position: absolute;
      left: 5%;
      top: 40%;
      p {
        margin: 10px 0;
      }
      .shop-head {
        padding: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #ddd;
        .shop-name {
          text-align: left;
        }
        .name {
          font-weight: bold;
        }
        .enname {
          font-size: 12px;
        }
      }
      .shop-sel-box {
        padding: 20px 0;
        border-bottom: 1px solid #ddd;
        .shop-sel {
          display: flex;
          align-items: center;
          margin-top: 10px;
          padding: 0 20px;
          .shop-sel-title {
            width: 48px;
            text-align: left;
            margin-right: 20px;
          }
          .shop-sel-item {
            min-width: 48px;
            padding: 2px 10px;
            border-radius: 10px;
            background-color: #eee;
            margin-right: 10px;
          }
          .shop-sel-active {
            background-color: rgb(12, 52, 186);
            color: #fff;
          }
        }
      }
      .shop-num {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20px;
        border-bottom: 1px solid #ddd;
        .shop-sel-num {
          width: 80px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .reduce,
          .add {
            width: 20px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border-radius: 50%;
            border: 1px solid #bbb;
          }
          .allow {
            background-color: rgb(12, 52, 186);
            color: #fff;
          }
        }
      }
      .shop-content {
        text-align: left;
        p {
          margin: 0;
          margin-top: 10px;
          padding: 0 20px;
        }
      }
    }
    .img-box {
      width: 100%;
      margin: 0 auto;
      img {
        width: 100%;
      }
    }
  }

  .bottom-box {
    width: 100%;
    padding: 2px 0;
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 60px;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    border-top: 1px solid #eee;
  }
}
</style>